(function (win, dom) {
    function eleId(id) {
        if (dom.getElementById) {
            eleId = function () {
                return dom.getElementById(id);
            };
            return dom.getElementById(id);
        } else {
            eleId = function () {
                return dom.querySelector('#' + id);
            };
            return dom.querySelector('#' + id);
        }
    }
    function constituteArr(code) {
        if (code.length === 1) return code[0];
        if (code.length > 1) return Array.prototype.concat.apply([], code);
        if (code.length < 1) return [];
    }
    function eleTag(tag) {
        if (dom.querySelectorAll) {
            eleTag = function () {
                var ele = dom.querySelectorAll(tag);
                return constituteArr(ele);
            };
            var ele = dom.querySelectorAll(tag);
            return constituteArr(ele);
        } else {
            eleTag = function () {
                var eletag = dom.getElementsByTagName(tag);
                return constituteArr(eletag);
            };
            var eletag = dom.getElementsByTagName(tag);
            return constituteArr(eletag);
        }
    }

    /* Fn getEleCls
*
* 兼容IE 通过类名取得元素
* 如果长度等于1 返回 当前索引为0 的元素
*
* */

    function getEleCls(sName) {
        if (dom.getElementsByClassName) {
            getEleCls = function () {
                var getCls = dom.getElementsByClassName(sName);
                return constituteArr(getCls);
            };
            var getCls = dom.getElementsByClassName(sName);
            return constituteArr(getCls);
        } else if (dom.querySelectorAll) {
            getEleCls = function () {
                var getQuer = dom.querySelectorAll('.' + sName);
                return constituteArr(getQuer);
            };
            var getQuer = dom.querySelectorAll('.' + sName);
            return constituteArr(getQuer);
        }
        /* else {
            getClass (sName, document);
        }
        */
    }
    var getarr = [];
    function getClass(className, node) {
        var nodeStr = node.childNodes;
        for (var i = 0; i < nodeStr.length; i++) {
            if (nodeStr.length > 1) {
                (nodeStr[i].nodeType === 1 && nodeStr[i].className === className) && getarr.push(nodeStr[i]);
                arguments.callee(className, nodeStr[i]);
            } else {
                (node.nodeType === 1 && node.className === className) && (getarr.push(node));
            }
        }
        return getarr;
    }


    /*
    * Fn getEle
    * 1.获得相对应得元素
    * 2.方法进行重写
    * 3.如果结果集长度为0 返回[];
    *   如果长度等于1 返回 当前索引为0 的元素
    *   如果长度大于1  返回  包含结果集的数组
    *   */

    function getEle(node) {
        if (node[0] === '.') {
            getEleCls(node.substring(1));
        } else if (node[0] === '#') {
            eleId(node.substring(1));
        } else {
            eleTag(node);
        }
    }
    //    去除传入值的前后空格
    var _str = '';
    getEle._trim = function _trim(str) {
        for (var i = 0; i < str.length; i++) {
            str[i] === ' ' ? _str += 0 : _str += 1;
        }
        return str.substring(_str.indexOf('1'), _str.lastIndexOf('1') + 1);
    };

    /*
    * 1.函数重载
    * 如果不传参数
    * 默认返回div
    * 如果传一个
    * 返回创建当前这个tag
    * 如果传2个参数
    * 返回创建设置第二个参数的class/id的第一个参数的tag
    * */
    // Fn createEle
    function create(tag, _name) {
        var crea = dom.createElement(tag);
        if (_name[0] === '.') {
            crea.setAttribute('class', _name.substring(1));
        } else if (_name[0] === '#') {
            crea.setAttribute('id', _name.substring(1));
        }
        return crea;
    }


    function createEle(tag, _name) {
        if (arguments.length === 2) {
            create(tag, _name);
        } else if (arguments.length === 1) {
            dom.createElement(tag);
        } else {
            dom.createElement('div');
        }
    }

    /* Fn attr
    * 1.函数重载
    * 根据参数个数不同
    * 返回查询后的结果
    * 或设置属性
    * 并返回设置成功与否
    * */
    function attr(doc, attrName, attrValue) {
        var _attr = dom.createAttribute(attrName);
        if (arguments.length === 3) {
            _attr.value = attrValue;
            doc.setAttributeNode(_attr);
            return true;
        } else if (arguments.length === 2) {
            return dom.getAttributeNode(attrName);
        } else {
            console.error('vaildty arguments needed');
        }
    }
    // <!--绑定事件兼容-->
    function addEvent(element, eType, handle) {
        if (element.addEventListener) {
            /* addEvent = function (element, eType, handle) {
                return element.addEventListener(eType, handle);
            };
            */
            element.addEventListener(eType, handle);
        } else if (element.attachEvent) {
            /* addEvent = function (element, eType, handle) {
                return element.attachEvent('on' + eType, handle);
            };
            */
            element.attachEvent('on' + eType, handle);
        } else {
            element['on' + eType] = handle;
        }
    }

    /*
     * @param dom [string]  传入一个节点
     * @param type [string]  绑定事件
     * @param fn    [string]  绑定方法
     * @param isBubble  [boolean] 是否冒泡
     */

    function bindEv(dom, type, fn, isBubble) {
        if (isBubble === true) {
            addEvent(dom, type, fn);
        } else {
            addEvent(dom, type, function (event) {
                var ev = event || window.event;
                fn();
                ev.stopPropagation() || (ev.cancelBubble = true);
            });
        }
    }

    /*
     *
     * @param dom
     * @param type
     * @param fn
     * @param bool
     */

    function newBindEv(dom, type, fn, bool) {
        if (typeof type === 'string' && typeof fn === 'function') {
            return bindEv(dom, type, fn, bool);
        } else if (type instanceof Array && typeof fn === 'function') {
            for (var i = 0; i < type.length; i++) {
                bindEv(dom, type[i], fn, bool);
            }
        } else if (type instanceof Array && fn instanceof Array) {
            for (var m = 0; m < type.length; m++) {
                for (var n = 0; n < fn.length; n++) {
                    if (m === n) {
                        bindEv(dom, type[m], fn[n], bool);
                    }
                }
            }
        } else if (fn instanceof Array && typeof type === 'string') {
            for (var g = 0; g < fn.length; g++) {
                bindEv(dom, type, fn[g], bool);
            }
        }
    }
    function addEv(dom, type, fn) {
        var oldFn;
        if (typeof dom['on' + type] === 'function') {
            //  取值
            oldFn = dom['on' + type];
        }
        // 重新赋值
        dom['on' + type] = function () {
            oldFn && oldFn();
            fn();
        };
    }


    win.getEleCls = getEleCls;
    win.eleId = eleId;
    win.getEle = getEle;
    win.bindEv = bindEv;
    win.addEv = addEv;
    win.eleTag = eleTag;
    win.getClass = getClass;
}(window, document, undefined));
